//! SyncJS.debug.js
//

(function($) {

Type.registerNamespace('SyncJS');

////////////////////////////////////////////////////////////////////////////////
// SyncJS._syncList

SyncJS._syncList = function SyncJS__syncList() {
    /// <field name="_uploadCnt" type="Number" integer="true" static="true">
    /// </field>
    /// <field name="_downloadCnt" type="Number" integer="true" static="true">
    /// </field>
    /// <field name="_noneCnt" type="Number" integer="true" static="true">
    /// </field>
    /// <field name="_uploadId" type="String" static="true">
    /// </field>
    /// <field name="_downloadId" type="String" static="true">
    /// </field>
    /// <field name="_noneId" type="String" static="true">
    /// </field>
    /// <field name="_logId" type="String" static="true">
    /// </field>
    /// <field name="_infoItemClass" type="String" static="true">
    /// </field>
    /// <field name="_infoNameClass" type="String" static="true">
    /// </field>
    /// <field name="_infos" type="Object" static="true">
    /// </field>
    /// <field name="_listsInitialized" type="Boolean" static="true">
    /// </field>
}
SyncJS._syncList.initLists = function SyncJS__syncList$initLists() {
    if (SyncJS._syncList._listsInitialized) {
        return;
    }
    Date.prototype.toJSON = function () {return "/Date(" + this.getTime() + "-0400)/"};
    $('th input[type=checkbox]').live('click', function() {
        var checkBoxes = $('input[type=checkbox]', $(this).closest('table'));
        var state = $(this).attr('checked') === 'checked';
        if (!state) {
            checkBoxes.removeAttr('checked');
        }
        else {
            checkBoxes.attr('checked', true);
        }
    });
    var headEl = $('#SyncDownList').closest('table').find('th.head2');
    $('<button/>').html('sync dates').click(function() {
        var jsonInfo = SyncJS._syncList.get_infoList();
        window.external.SyncDates(jsonInfo);
    }).css('float', 'right').prependTo(headEl);
    SyncJS._syncList._listsInitialized = true;
}
SyncJS._syncList.get_infoList = function SyncJS__syncList$get_infoList() {
    /// <value type="String"></value>
    SyncJS._syncList._captureUI();
    return SyncJS._syncList._dictify(SyncJS._syncList._infos);
}
SyncJS._syncList.clearLists = function SyncJS__syncList$clearLists() {
    $('#SyncUpList' + ',' + '#SyncDownList' + ',' + '#SyncNoneList').each(function() {
        $(this).empty().closest('table').hide();
    });
}
SyncJS._syncList.fillLists = function SyncJS__syncList$fillLists(infos) {
    /// <param name="infos" type="Array" elementType="SyncInfoJS">
    /// </param>
    SyncJS._syncList._infos = SyncJS._syncList._convertToDict(infos);
    SyncJS._syncList.set__counts(0);
    var $dict1 = SyncJS._syncList._infos;
    for (var $key2 in $dict1) {
        var pair = { key: $key2, value: $dict1[$key2] };
        var info = pair.value;
        if (info.Upload) {
            SyncJS._syncList._addUploadFile(info);
        }
        if (info.Download) {
            SyncJS._syncList._addDownloadFile(info);
        }
        if (!info.Upload && !info.Download) {
            SyncJS._syncList._addNoneFile(info);
        }
    }
    if (!!SyncJS._syncList._uploadCnt) {
        $('#SyncUpList').parent().show();
    }
    else {
        $('#SyncUpList').parent().hide();
    }
    if (!!SyncJS._syncList._downloadCnt) {
        $('#SyncDownList').parent().show();
    }
    else {
        $('#SyncDownList').parent().hide();
    }
    if (!!SyncJS._syncList._noneCnt) {
        $('#SyncNoneList').parent().show();
    }
    else {
        $('#SyncNoneList').parent().hide();
    }
    var logText = window.external.LogText();
    $('#SyncLog').html(logText);
    if (window.external.AutoSync()) {
        window.external.Sync();
    }
}
SyncJS._syncList._convertToDict = function SyncJS__syncList$_convertToDict(infos) {
    /// <param name="infos" type="Array" elementType="SyncInfoJS">
    /// </param>
    /// <returns type="Object"></returns>
    var dict = {};
    var $enum1 = ss.IEnumerator.getEnumerator(infos);
    while ($enum1.moveNext()) {
        var info = $enum1.current;
        dict[info.Name] = info;
    }
    return dict;
}
SyncJS._syncList.get__counts = function SyncJS__syncList$get__counts() {
    /// <value type="Number" integer="true"></value>
    return 0;
}
SyncJS._syncList.set__counts = function SyncJS__syncList$set__counts(value) {
    /// <value type="Number" integer="true"></value>
    SyncJS._syncList._uploadCnt = SyncJS._syncList._downloadCnt = SyncJS._syncList._noneCnt = value;
    return value;
}
SyncJS._syncList._addUploadFile = function SyncJS__syncList$_addUploadFile(info) {
    /// <param name="info" type="SyncJS.SyncInfoJS">
    /// </param>
    SyncJS._syncList._uploadCnt++;
    var tBody = $('#SyncUpList');
    var tRow = $('<tr/>').appendTo(tBody);
    SyncJS._syncList._fillTableRow(info, tRow);
    if (info.Upload) {
        $('input[type=checkbox]', tRow).attr('checked', 'true');
    }
}
SyncJS._syncList._addDownloadFile = function SyncJS__syncList$_addDownloadFile(info) {
    /// <param name="info" type="SyncJS.SyncInfoJS">
    /// </param>
    SyncJS._syncList._downloadCnt++;
    var tBody = $('#SyncDownList');
    var tRow = $('<tr/>').appendTo(tBody);
    SyncJS._syncList._fillTableRow(info, tRow);
    if (info.Download) {
        $('input[type=checkbox]', tRow).attr('checked', 'true');
    }
}
SyncJS._syncList._addNoneFile = function SyncJS__syncList$_addNoneFile(info) {
    /// <param name="info" type="SyncJS.SyncInfoJS">
    /// </param>
    SyncJS._syncList._noneCnt++;
    var tBody = $('#SyncNoneList');
    var tRow = $('<tr/>').appendTo(tBody);
    SyncJS._syncList._fillTableRow(info, tRow);
}
SyncJS._syncList._fillTableRow = function SyncJS__syncList$_fillTableRow(info, tRow) {
    /// <param name="info" type="SyncJS.SyncInfoJS">
    /// </param>
    /// <param name="tRow" type="jQueryObject">
    /// </param>
    tRow.addClass('InfoItemClass');
    var lDate = new Date(info.LDate.getTime());
    var rDate = new Date(info.RDate.getTime());
    $('<td/>').html("<input type='checkbox'>").appendTo(tRow);
    $('<td/>').html(info.Name).addClass('InfoNameClass').appendTo(tRow);
    $('<td/>').html(lDate.format('yyyy-MM-dd HH:mm:ss')).appendTo(tRow);
    $('<td/>').html(rDate.format('yyyy-MM-dd HH:mm:ss')).appendTo(tRow);
    $('<td/>').html(info.SizeDelta.toString()).appendTo(tRow);
}
SyncJS._syncList._captureUI = function SyncJS__syncList$_captureUI() {
    $('.' + 'InfoItemClass').each(function() {
        var name = $('.' + 'InfoNameClass', $(this)).html();
        SyncJS._syncList._infos[name].Transfer = $('input[type=checkbox]', $(this)).attr('checked') === 'checked';
    });
}
SyncJS._syncList._dictify = function SyncJS__syncList$_dictify(dict) {
    /// <param name="dict" type="Object">
    /// </param>
    /// <returns type="String"></returns>
    var ot = '[';
    var cnt = 0;
    var $dict1 = dict;
    for (var $key2 in $dict1) {
        var pair = { key: $key2, value: $dict1[$key2] };
        if (cnt++ > 0) {
            ot += ',';
        }
        ot += '{';
        ot += '"Key": ' + JSON.stringify(pair.key);
        ot += ', "Value": ' + JSON.stringify(pair.value);
        ot += '}';
    }
    ot += ']';
    return ot;
}


////////////////////////////////////////////////////////////////////////////////
// SyncJS.Css

SyncJS.Css = function SyncJS_Css() {
}
SyncJS.Css.addStyles = function SyncJS_Css$addStyles() {
    DynamicCss.addCssRule('#out', { fontFamily: 'calibri,helvetica,arial', fontSize: '12pt', paddingLeft: 35, color: 'purple' });
    DynamicCss.addCssRule('body', { fontFamily: 'calibri,helvetica,arial', fontSize: '12pt', paddingLeft: 35, color: 'purple' });
}


////////////////////////////////////////////////////////////////////////////////
// SyncJS.Play

SyncJS.Play = function SyncJS_Play() {
}
SyncJS.Play.TestMe = function SyncJS_Play$TestMe() {
    $('#out').css('backgroundColor', 'red');
}


Type.registerNamespace('SyncJs');

////////////////////////////////////////////////////////////////////////////////
// Main

window.Start = function Main$Start() {
    window.external.GetSyncInfo();
    SyncJS._syncList.clearLists();
    $('#SyncUpMsg').html('Loading ...');
    SyncJS._syncList.initLists();
}
window.InfoList = function Main$InfoList() {
    /// <returns type="String"></returns>
    return SyncJS._syncList.get_infoList();
}
window.ShowList = function Main$ShowList(jsonResult) {
    /// <param name="jsonResult" type="String">
    /// </param>
    var syncInfosJs = eval('(' + jsonResult + ')');
    $('#SyncUpMsg').empty();
    SyncJS._syncList.fillLists(syncInfosJs);
}
window.ShowError = function Main$ShowError(errorMsg) {
    /// <param name="errorMsg" type="String">
    /// </param>
    $('#SyncUpMsg').text(errorMsg);
}


SyncJS._syncList.registerClass('SyncJS._syncList');
SyncJS.Css.registerClass('SyncJS.Css');
SyncJS.Play.registerClass('SyncJS.Play');
SyncJS._syncList._uploadCnt = 0;
SyncJS._syncList._downloadCnt = 0;
SyncJS._syncList._noneCnt = 0;
SyncJS._syncList._infos = null;
SyncJS._syncList._listsInitialized = false;
})(jQuery);

//! This script was generated using Script# v0.7.4.0
